//  定义vuex的user2模块
import { SysLogin } from '@/api/login'
import { sysProfile, sysUser } from '@/api/user'
import router from '@/router'
import { asyncRoutes, constantRoutes } from '@/router/index'
export default {
  namespaced: true,
  state: {
    token: '',
    userInfo: ''
  },
  mutations: {
    setToken(state, value) {
      state.token = value
    },
    setUserInfo(state, value) {
      state.userInfo = value
    },
    //  退出处理
    logout(state) {
      state.token = ''
      state.userInfo = ''
      router.push('/login?back' + location.href.split('#')[1])
    }
  },
  actions: {
    //  登录处理 调用api接口
    async toLogin({ commit }, from) {
      const res = await SysLogin(from)
      commit('setToken', res.data.data)
      console.log(3)
    },
    //  获取用户信息
    async getUserInfo({ commit }) {
      const res = await sysProfile()
      const res2 = await sysUser(res.data.data.userId)
      const res3 = Object.assign({}, res.data.data, res2.data.data)
      commit('setUserInfo', res3)
      console.log('res3', res3)
      const newArr = asyncRoutes.filter((item) => {
        return res3.roles.menus.includes(item.meta.name)
      })
      //  添加:router有一个方法叫addRoutes(需要添加的路由)
      newArr.push({ path: '*', redirect: '/404', hidden: true })
      router.addRoutes(newArr)

      // 修改vuex的路由数据
      commit('routes/setRoutes', [...constantRoutes, ...newArr], { root: true })
      console.log('newArr', newArr)
      console.log('获取用户基本信息值', res)
    }
  },
  getters: {}
}
